<?php
/**
 * This file contains Pagination class
 * @package Realivent-Facebook
 * @author Vinod Kumar <30.vinodkumar@gmail.com>
 * @version 1.0
 *
 */

/*
*
 * Pagination class
 * This class creates pagination links like flickr.com
 * @package Realivent-Facebook
 * @author Vinod Kumar <30.vinodkumar@gmail.com>
 * @version 1.0
 *
 */
class Paging {

	/**
	 * Generates/Renders paging links HTML from given parameters
	 * Also includes style for paging HTML
	 *
	 * @param int	$totalRecords
	 * @param int	$page
	 * @param int	$limit
	 * @return string
	 */
	public static function getPaging($totalRecords, $limit=10, $params, $url="/", $img_url='/') {

		$page = isset($_REQUEST["page"])?(int)$_REQUEST["page"]:0;
		foreach ($params as $k=>$v) {
			$params[$k]=stripslashes($v);
		}
		$query = http_build_query($params);
		$query = ($query)?"&$query":$query;

		$adjacents = 1;

		/* Setup vars for query. */
		if($page)
		$start = ($page - 1) * $limit; 			//first item to display on this page
		else
		$start = 0;								//if no page var is given, set start to 0

		/* Setup page vars for display. */
		if ($page == 0) $page = 1;					//if no page var is given, default to 1.
		$prev = ($page - 1)<1?1:($page-1);							//previous page is page - 1
		$next = $page + 1;							//next page is page + 1
		$lastpage = ceil($totalRecords/$limit);		//lastpage is = total pages / items per page, rounded up.
		$lpm1 = $lastpage - 1;						//last page minus 1

		/*
		Now we apply our rules and draw the pagination object.
		We're actually saving the code to a variable in case we want to draw it more than once.
		*/
		$pagination = "";
		if($lastpage > 1)
		{
			$pagination = '';
			$pagination .= "<div id='page-navigation'>";
			//			$pagination .= "<a href='$url?page=1' class='previous-link'><img src='$img_url/images/icon-previous.png' alt='First' title='First' /></a>";
			$pagination .= "<a href='$url?page=$prev' class='previous-link'><img src='$img_url/images/icon-previous.png' alt='First' title='First' /></a>";
			//previous button
			if ($page > 1)
			$pagination .= "<a href='$url?page=$prev$query' class='previous-link'>Previous</a>";
			else
			$pagination .= "<a href='$url?page=$prev$query' class='previous-link'>Previous</a>";

			//pages
			if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
			{
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
					$pagination .= "<span class='active-page'>$counter</span>";
					else
					$pagination .= "<a href='$url?page=$counter$query'>$counter</a>";
				}
			}
			elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
			{
				//close to beginning; only hide later pages
				if($page < 1 + ($adjacents * 2))
				{
					for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='$url?page=$counter$query'>$counter</a>";
					}
					$pagination .= "...";
					$pagination .= "<a href='$url?page=$lpm1$query'>$lpm1</a>";
					$pagination .= "<a href='$url?page=$lastpage$query'>$lastpage</a>";
				}
				//in middle; hide some front and some back
				elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
				{
					$pagination .= "<a href='$url?page=1$query'>1</a>";
					$pagination .= "<a href='$url?page=2$query'>2</a>";
					$pagination .= "...";
					for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						elseif($counter!=2)
						$pagination .= "<a href='$url?page=$counter$query'>$counter</a>";
					}
					$pagination .= "...";
					$pagination .= "<a href='$url?page=$lpm1$query'>$lpm1</a>";
					$pagination .= "<a href='$url?page=$lastpage$query'>$lastpage</a>";
				}
				//close to end; only hide early pages
				else
				{
					$pagination .= "<a href='$url?page=1$query'>1</a>";
					$pagination .= "<a href='$url?page=2$query'>2</a>";
					$pagination .= "...";
					for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='$url?page=$counter$query'>$counter</a>";
					}
				}
			}

			$last = ceil($totalRecords/$limit);
			$next = ($next>$last)?$last:$next;
			//next button
			if ($page < $counter - 1)
			$pagination .= "<a href='$url?page=$next$query' class='next-link'>Next</a>";
			else
			$pagination .= "<a href='$url?page=$next$query' class='next-link'>Next</a>";
			$pagination .= "<a href='$url?page=$next$query' class='next-link'><img src='$img_url/images/icon-next.png' alt='Last' title='Last' /></a>";
			//			$pagination .= "<a href='$url?page=$last$query' class='next-link'><img src='$img_url/images/icon-next.png' alt='Last' title='Last' /></a>";
			$pagination .= "</div>";
		}
		return $pagination;
	}

	public static function extractQS($ignores=array("page_")) {

		$temp = explode("&", $_SERVER['QUERY_STRING']);
		if(!$temp) return "";

		$output = array();
		foreach ($temp as $k=>$str) {
			foreach ($ignores as $ignr) {
				if(strpos($str, $ignr."=") !== FALSE) unset($temp[$k]);
			}
		}
		return implode("&", $temp);
	}

	public static function getPaging1($totalRecords, $limit=10, $params, $img_url='/') {

		$page = isset($_REQUEST["page_"])?(int)$_REQUEST["page_"]:0;
		unset($params['page_']);
		foreach ($params as $k=>$v) {
			$params[$k]=stripslashes($v);
		}
		
		$query = http_build_query($params);
		$query = ($query)?"&$query":$query;

//		$url = $_SERVER['SCRIPT_NAME']."?".self::extractQS(array("page_"));
		$url = $_SERVER['SCRIPT_NAME']."?".$query;
		$adjacents = 1;

		/* Setup vars for query. */
		if($page)
		$start = ($page - 1) * $limit; 			//first item to display on this page
		else
		$start = 0;								//if no page var is given, set start to 0

		/* Setup page vars for display. */
		if ($page == 0) $page = 1;					//if no page var is given, default to 1.
		$prev = ($page - 1)<1?1:($page-1);							//previous page is page - 1
		$next = $page + 1;							//next page is page + 1
		$lastpage = ceil($totalRecords/$limit);		//lastpage is = total pages / items per page, rounded up.
		$lpm1 = $lastpage - 1;						//last page minus 1

		/*
		Now we apply our rules and draw the pagination object.
		We're actually saving the code to a variable in case we want to draw it more than once.
		*/
		$pagination = "";
		if($lastpage > 1)
		{
			$pagination = '';
			$pagination .= "<div id='page-navigation'>";
//			$pagination .= "<a href='$url&page_=1' class='previous-link'><img src='$img_url/images/icon-previous.png' alt='First' title='First' /></a>";
			//previous button
			if ($page > 1)
			$pagination .= "<a href='$url&page_=$prev' class='previous-link'>&laquo;</a>";
			else
			$pagination .= "<a href='$url&page_=$prev' class='previous-link'>&laquo;</a>";

			//pages
			if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
			{
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
					$pagination .= "<span class='active-page'>$counter</span>";
					else
					$pagination .= "<a href='$url&page_=$counter'>$counter</a>";
				}
			}
			elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
			{
				//close to beginning; only hide later pages
				if($page < 1 + ($adjacents * 2))
				{
					for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='$url&page_=$counter'>$counter</a>";
					}
					$pagination .= "...";
					$pagination .= "<a href='$url&page_=$lpm1'>$lpm1</a>";
					$pagination .= "<a href='$url&page_=$lastpage'>$lastpage</a>";
				}
				//in middle; hide some front and some back
				elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
				{
					$pagination .= "<a href='$url&page_=1'>1</a>";
					$pagination .= "<a href='$url&page_=2'>2</a>";
					$pagination .= "...";
					for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='$url&page_=$counter'>$counter</a>";
					}
					$pagination .= "...";
					$pagination .= "<a href='$url&page_=$lpm1'>$lpm1</a>";
					$pagination .= "<a href='$url&page_=$lastpage'>$lastpage</a>";
				}
				//close to end; only hide early pages
				else
				{
					$pagination .= "<a href='$url&page_=1'>1</a>";
					$pagination .= "<a href='$url&page_=2'>2</a>";
					$pagination .= "...";
					for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='$url&page_=$counter'>$counter</a>";
					}
				}
			}

			$last = ceil($totalRecords/$limit);
			$next = ($next>$last)?$last:$next;
			//next button
			if ($page < $counter - 1)
			$pagination .= "<a href='$url&page_=$next' class='next-link'>&raquo;</a>";
			else
			$pagination .= "<a href='$url&page_=$next' class='next-link'>&raquo;</a>";
//			$pagination .= "<a href='$url&page_=$last' class='next-link'><img src='$img_url/images/icon-next.png' alt='Last' title='Last' /></a>";
			$pagination .= "</div>";
		}
		return $pagination;
	}
	
	public static function getPagingFBML($totalRecords, $limit=10, $params, $img_url='/') {

		$page = isset($_REQUEST["page_"])?(int)$_REQUEST["page_"]:0;
		foreach ($params as $k=>$v) {
			$params[$k]=stripslashes($v);
		}

		$url = $_SERVER['SCRIPT_NAME']."?".self::extractQS(array("page_"));
		$adjacents = 1;

		/* Setup vars for query. */
		if($page)
		$start = ($page - 1) * $limit; 			//first item to display on this page
		else
		$start = 0;								//if no page var is given, set start to 0

		/* Setup page vars for display. */
		if ($page == 0) $page = 1;					//if no page var is given, default to 1.
		$prev = ($page - 1)<1?1:($page-1);							//previous page is page - 1
		$next = $page + 1;							//next page is page + 1
		$lastpage = ceil($totalRecords/$limit);		//lastpage is = total pages / items per page, rounded up.
		$lpm1 = $lastpage - 1;						//last page minus 1

		/*
		Now we apply our rules and draw the pagination object.
		We're actually saving the code to a variable in case we want to draw it more than once.
		*/
		$pagination = "";
		if($lastpage > 1)
		{
			$pagination = '';
			$pagination .= "<div id='page-navigation'>";
//			$pagination .= "<a href='$url&page_=1' class='previous-link'><img src='$img_url/images/icon-previous.png' alt='First' title='First' /></a>";
			//previous button
			if ($page > 1)
			$pagination .= "<a href='#' onclick='loadListing($prev)' class='previous-link'>Prev</a>";
			else
			$pagination .= "<a href='#' onclick='loadListing($prev)' class='previous-link'>Prev</a>";

			//pages
			if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
			{
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
					$pagination .= "<span class='active-page'>$counter</span>";
					else
					$pagination .= "<a href='#' onclick='loadListing($counter)'>$counter</a>";
				}
			}
			elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
			{
				//close to beginning; only hide later pages
				if($page < 1 + ($adjacents * 2))
				{
					for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='#' onclick='loadListing($counter)'>$counter</a>";
					}
					$pagination .= "...";
					$pagination .= "<a href='$url&page_=$lpm1'>$lpm1</a>";
					$pagination .= "<a href='#' onclick='loadListing($lastpage)'>$lastpage</a>";
				}
				//in middle; hide some front and some back
				elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
				{
					$pagination .= "<a href='$url&page_=1'>1</a>";
					$pagination .= "<a href='$url&page_=2'>2</a>";
					$pagination .= "...";
					for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='#' onclick='loadListing($counter)'>$counter</a>";
					}
					$pagination .= "...";
					$pagination .= "<a href='#' onclick='loadListing($lpm1)'>$lpm1</a>";
					$pagination .= "<a href='#' onclick='loadListing($lastpage)'>$lastpage</a>";
				}
				//close to end; only hide early pages
				else
				{
					$pagination .= "<a href='#' onclick='loadListing(1)'>1</a>";
					$pagination .= "<a href='#' onclick='loadListing(2)'>2</a>";
					$pagination .= "...";
					for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page)
						$pagination .= "<span class='active-page'>$counter</span>";
						else
						$pagination .= "<a href='#' onclick='loadListing($counter)'>$counter</a>";
					}
				}
			}

			$last = ceil($totalRecords/$limit);
			$next = ($next>$last)?$last:$next;
			//next button
			if ($page < $counter - 1)
			$pagination .= "<a href='#' onclick='loadListing($next)' class='next-link'>Next</a>";
			else
			$pagination .= "<a href='#' onclick='loadListing($next)' class='next-link'>Next</a>";
//			$pagination .= "<a href='#' onclick='loadListing($last)' class='next-link'><img src='$img_url/images/icon-next.png' alt='Last' title='Last' /></a>";
			$pagination .= "</div>";
		}
		return $pagination;
	}

	public static function getPagingSingle($totalRecords, $limit=10, $params, $url="/", $img_url='/') {

		$page = isset($_REQUEST["page"])?(int)$_REQUEST["page"]:0;

		$query = http_build_query($params);
		$query = ($query)?"&$query":$query;

		$adjacents = 3;

		/* Setup vars for query. */
		if($page)
		$start = ($page - 1) * $limit; 			//first item to display on this page
		else
		$start = 0;								//if no page var is given, set start to 0

		/* Setup page vars for display. */
		if ($page == 0) $page = 1;					//if no page var is given, default to 1.
		$prev = ($page - 1)<1?1:($page-1);							//previous page is page - 1
		$next = $page + 1;							//next page is page + 1

		/*
		Now we apply our rules and draw the pagination object.
		We're actually saving the code to a variable in case we want to draw it more than once.
		*/
		$pagination = "";
		if($page >= 1)
		{
			$pagination = '';
			$pagination .= "<div id='page-navigation'>";
			//previous button
			if ($page > 1)
			$pagination .= "<a href='$url?page=$prev$query' class='previous-link'><img src='$img_url/images/btnPreviousListing.gif' alt='Previous Listing' title='Previous Listing' /></a>";
			else
			$pagination .= "";

			$pagination .= "<a href='{$_REQUEST['back']}' class='previous-link' id='back_link'><img src='$img_url/images/back.gif' alt='Back to Results' title='Back to Results' /></a>";

			$next = $page+1;
			//next button
			if (ceil($totalRecords/$limit) > $page)
			$pagination .= "<a href='$url?page=$next$query' class='previous-link'><img src='$img_url/images/btnNextListing.gif' alt='Next Listing' title='Next Listing' /></a>";
			$pagination .= "</div>";
		}
		return $pagination;
	}
}
?>